package com.foreveross.proxyip.application.impl.operate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.proxyip.application.operate.IpWebsiteEntityApplication;
import com.foreveross.proxyip.application.operate.vo.IpWebsiteEntityVo;
import com.foreveross.proxyip.core.http.ipdoload.IpDoloadManager;
import com.foreveross.proxyip.entity.IpWebsiteEntity;
import com.foreveross.proxyip.entity.ProxyIpBean;
import com.foreveross.proxyip.infra.IProxyIpOriginal;
import com.foreveross.proxyip.infra.PageData;
import com.foreveross.proxyip.infra.base.ddd.repository.IBaseRepository;
import com.foreveross.proxyip.util.IpWebiteUtil;

@Transactional
@Service
public class IpWebsiteEntityApplicationImpl implements IpWebsiteEntityApplication {

	
	@Resource(name = "entityHibernateRepository")
	IBaseRepository baseRepository;
	
	@Autowired
	private IpDoloadManager ipDoloadManager;

	@Resource(name = "webSiteDoload")
	private IProxyIpOriginal webSiteDoload; 
	
	@Override
	public PageData findAllIpWebsite(IpWebsiteEntityVo vo, int pageIndex,
			int pageLen) throws Exception {
		if(pageIndex<=0){
			pageIndex=PageData.DEFAULT_PAGEINDEX;
		}
		if(pageLen<=0){
			pageLen=PageData.DEFAULT_PAGELEN;
		}
		List<Object> values=new ArrayList();
		//获取所有IpWebsiteEntity实体
		List<IpWebsiteEntity> websites = baseRepository.findAll(IpWebsiteEntity.class);
		for(IpWebsiteEntity e:websites)
		{
			IpWebsiteEntityVo v=new IpWebsiteEntityVo(
					e.getId(),
					e.getCode(),
					e.getCreateTime(),
					e.getEncoding(),
					e.getLastTime(),
					e.getName(),
					e.getRowRegex(),
					e.getSplitTime(),
					e.getState(),
					e.getUrlRegex(),
					e.getUserName(),
					e.getOrderid());
			values.add(v);
		}
		PageData p=new PageData();
		p.setPageIndex(pageIndex);
		p.setPageLen(pageLen);
		p.setResult(values);
		p.setDataSize(values.size());
		int pageCount=(int)values.size()/pageLen+((int)values.size()%pageLen>0?1:0);
		p.setPageCount(pageCount);
		return p;
	}
	
		/**
		 * 增加代理网站配置
		 */
	@Override
	public IpWebsiteEntity addIpWebsite(IpWebsiteEntityVo vo) throws Exception {
		IpWebsiteEntity addWebsite=new IpWebsiteEntity();
			addWebsite.setCode(vo.getCode());
			addWebsite.setEncoding(vo.getEncoding());
			addWebsite.setName(vo.getName());
			addWebsite.setCreateTime(new Date());
			addWebsite.setLastTime(new Date());
			addWebsite.setRowRegex(vo.getRowRegex());
			addWebsite.setSplitTime(vo.getSplitTime());
			addWebsite.setState(vo.getState());
			addWebsite.setUrlRegex(vo.getUrlRegex());
			addWebsite.setUserName(vo.getUserName());
			addWebsite.setOrderid(vo.getOrderid());

			baseRepository.create(addWebsite);
			ipDoloadManager.reloadIpWebsites();
	
			
			return addWebsite;

		
	}

	     /**
	      * 修改代理网站配置
	      */
	@Override
	public IpWebsiteEntity updateIpWebsite(IpWebsiteEntityVo vo)
			throws Exception {

		IpWebsiteEntity ipWebsiteEntity=baseRepository.find(IpWebsiteEntity.class, vo.getId());
		ipWebsiteEntity.setCode(vo.getCode());
		ipWebsiteEntity.setEncoding(vo.getEncoding());
		ipWebsiteEntity.setName(vo.getName());
		ipWebsiteEntity.setRowRegex(vo.getRowRegex());
		ipWebsiteEntity.setSplitTime(vo.getSplitTime());
		ipWebsiteEntity.setUrlRegex(vo.getUrlRegex());
		ipWebsiteEntity.setState(vo.getState());
		ipWebsiteEntity.setLastTime(new Date());
		baseRepository.update(ipWebsiteEntity);
		ipDoloadManager.reloadIpWebsites();
		
		return ipWebsiteEntity;
	}

		/**
		 * 删除代理网站
		 */
	@Override
	public void deleteIpWebsite(IpWebsiteEntityVo vo)
			throws Exception {
		IpWebsiteEntity ipWebsiteEntity=baseRepository.find(IpWebsiteEntity.class, vo.getId());
		baseRepository.delete(ipWebsiteEntity);
	}
	
	
	/**
	 * 保证配置的网站能下载IP
	 * @param website
	 * @return
	 */
	@Override
	public List<String> checkWebsite(IpWebsiteEntityVo vo) {
		ProxyIpBean proxyIp = new ProxyIpBean();
		List<String> result=new ArrayList<String>();
		Object[] ips = null;
		IpWebsiteEntity website=new IpWebsiteEntity();
		website.setCode(vo.getCode());
		website.setEncoding(vo.getEncoding());
		website.setName(vo.getName());
		website.setRowRegex(vo.getRowRegex());
		website.setSplitTime(vo.getSplitTime());
		website.setState(vo.getState());
		website.setUrlRegex(vo.getUrlRegex());
		website.setOrderid(vo.getOrderid());

		
			   try {
					
						   ips=webSiteDoload.httpGetIPS(website, 1);
					   
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			   
		for(Object downLoadRow:ips ){
			String downLoad=downLoadRow.toString().trim();
			result.add("下载行为："+downLoad+"******");
			try {
				IpWebiteUtil.explainRow(proxyIp, website, downLoad);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		String explainResult="解析结果为：ip="+proxyIp.getIp()+" "+"端口="+proxyIp.getPort();
		
		result.add(explainResult);
        	
	return result;
	}
}
